-- Selector
-- 方块选择栏

Selector = class()
Selector.MaxItem = 9
Selector.width = 80
Selector.texImage = readImage(Tex)

function Selector:init()
    self.items = {{id=Block.spooge.id,count = 10},{id=Block.water.id,count=10},
    {id=Block.stone.id,count = 100},{id=Block.ice.id,count = 10},
    {id=Block.soilSand.id,count = 100},{id=Block.lava.id,count = 10},
    {id=Block.leaf.id,count = 10}}
    self.point = 1
    self.left = 305
    self.t = Tesselator()
end

-- 加入方块
function Selector:add(tileid,count)
    for i,v in ipairs(self.items) do
        if v.id == tileid then
            v.count = v.count + count
            return
        end
    end
    local i = {}
    i.id = tileid
    i.count = count
    table.insert(self.items,i)
end

-- 加入一个方块
function Selector:addOneTile(tileid)
    self:add(tileid,1)
end

-- 移出一个选择的方块
function Selector:removeOneSelectTile()
    if self:hasSelectTile() then
        self.items[self.point].count = self.items[self.point].count - 1
        if self.items[self.point].count == 0 then
            self.items[self.point] = nil
        end
    end
end

-- 获得选择的方块Id
function Selector:getSelectTileId()
    if self:hasSelectTile() then
        return self.items[self.point].id
    end
    return Block.air.id
end

-- 是否选择了方块
function Selector:hasSelectTile()
    if self.items[self.point] == nil then return false end
    return true
end

-- 选择框向前移
function Selector:forward()
    self.point = self.point + 1
    if self.point > Selector.MaxItem then
        self.point = 1
    end
end

function Selector:render()
    pushStyle()
    font("Courier-Bold")
    fontSize(14)
    fill(255, 255, 255, 255)
    textMode(CENTER)
    tint(255, 255, 255, 255)
    sprite("Project:widgets",self.left,0,Selector.width * Selector.MaxItem)
    translate(0,0,50)
    for i = 1,Selector.MaxItem do
        if self.items[i] ~= nil then
            self:renderTile(i,self.items[i].id)
            text(tostring(self.items[i].count),self.left + Selector.width * i - 25,17)
        end
    end
    translate(0,0,50)
    sprite("Project:selection",self.left + Selector.width * (self.point - 1) - 6,-4,Selector.width + 14)
    popStyle()
end

function Selector:touched(touch)
    local x,y = touch.x,touch.y
    if not self:isInRect(x,y,self.left,Selector.width * Selector.MaxItem,Selector.width) then
        return false
    end
    for i = 0, Selector.MaxItem - 1 do
        if self:isInRect(x,y,self.left + Selector.width * i,Selector.width,Selector.width) then
            self.point = i + 1
        end
    end
    return true
end

function Selector:isInRect(tx,ty,x,w,h)
    return (tx > x) and (tx < x + w) and (ty < h)
end

function Selector:renderTile(i,tileid)
    local b = Block.blocks[tileid]
    self.t:begin()
    self.t:bindTex(Tex)
    self.t:brightb(204)
    Block.renderTexture(self.t,0,0,0,Front,b:getTex(Front))
    self.t:brightb(255)
    Block.renderTexture(self.t,0,0,0,Top,b:getTex(Top))
    self.t:brightb(178)
    Block.renderTexture(self.t,0,0,0,Right,b:getTex(Right))
    self.t:End()
    pushMatrix()
    resetMatrix()
    translate(self.left + (Selector.width - 0.5) * (i - 1 + 0.4) + 10.5,Selector.width * 0.4 + 4,500)
    rotate(25,1,0,0)
    rotate(-45,0,1,0)
    scale(37)
    self.t:render()
    popMatrix()
end
